local jugu = fk.CreateSkill {
  name = "lvshi__jugu",
  anim_type = "control",
}

Fk:loadTranslationTable{
  ["lvshi__jugu"] = "居毂",
  [":lvshi__jugu"] = "其他角色的出牌阶段开始时，你可重铸一至三张基本牌，令其弃置等量的牌，否则其于此阶段使用非装备牌的点数之和不能大于这些牌的点数之和。",
  ["#lvshi__jugu-choose"] = "居毂：请选择要重铸的基本牌",
  ["#lvshi__jugu-phase"] = "%arg 阶段内使用非装备牌的点数之和",
  ["@lvshi__jugu-phase"] = "居毂",

  ["$lvshi__jugu1"] = "虎步南北，克敌制胜！",
  ["$lvshi__jugu2"] = "司兵聚将，绸缪于先！",
}

jugu:addEffect(fk.EventPhaseStart, {
  can_trigger = function(self, event, target, player, data)
    return target ~= player and player:hasSkill(jugu.name) and target.phase == Player.Play
  end,
  on_cost = function(self, event, target, player, data)
    local param = {
      min_num = 1,
      max_num = 3,
      include_equip = false,
      skill_name = jugu.name,
      pattern = ".|.|.|.|.|basic|.",
      prompt = "#lvshi__jugu-choose",
      cancelable = true,
    }
    local to_recast = player.room:askToCards(player, param)
    if #to_recast > 0 then
      event:setCostData(self, {cards = to_recast})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to_recast = event:getCostData(self).cards
    local n = 0
    for _, id in ipairs(to_recast) do
      n = n + Fk:getCardById(id).number
    end
    room:recastCard(to_recast, player, jugu.name)
    local discards = room:askToDiscard(target, {
      min_num = #to_recast,
      max_num = #to_recast,
      include_equip = false,
      skill_name = jugu.name,
      cancelable = true,
    })
    if #discards == 0 then
      room:setPlayerMark(target, "lvshi__jugu-phase", n)
      room:setPlayerMark(target, "@lvshi__jugu-phase", "0".."/"..target:getMark("lvshi__jugu-phase"))
    end
  end,
})

jugu:addEffect(fk.CardUsing, {
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(self.name) and target:getMark("lvshi__jugu-phase") ~= 0 and 
     target:getMark("@lvshi__jugu-phase") ~= 0 and data.card.type ~= Card.TypeEquip
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if data.card.number > 0 then
      room:addPlayerMark(target, "jugu-phase", data.card.number)
    end
    room:setPlayerMark(target, "@lvshi__jugu-phase", target:getMark("jugu-phase").."/"..target:getMark("lvshi__jugu-phase"))
  end,
})

jugu:addEffect("prohibit", {
  prohibit_use = function(self, player, card)
    if player:getMark("@lvshi__jugu-phase") ~= 0 and player:getMark("lvshi__jugu-phase") ~= 0 and card.type ~= Card.TypeEquip then
      return player:getMark("jugu-phase") + card.number > player:getMark("lvshi__jugu-phase")
    end
  end,
})

return jugu